const db = require('../db/db')
module.exports = {
    // 快速消费
    FASTCONSUME: async (ctx, next) => {
        const params=  ctx.request.body
        let createTime = new Date()
        try {
            let [res] = await db.promise().query('insert into myorder(id,userId,userName,packageName,packagePrice,packageNum,payWay,totalPrice,cutPrice,staff,getScore,notes,createTime) values(?,?,?,?,?,?,?,?,?,?,?,?,?)',
           [params.id,params.userId,params.userName,params.packageName,params.packagePrice,params.packageNum,params.payWay,params.totalPrice,params.cutPrice,params.staff,params.getScore,params.notes,createTime])
            if(res.affectedRows > 0){
                ctx.body = {
                    code:200,
                    msg:'结算成功'
                }
               }
           } catch (error) {
                ctx.body = {
                    code:201,
                    msg:'结算失败请联系管理员' +error
                }
           }
    },
    // 商品消费
    GOODSCONSUME: async (ctx, next) => {
        const params=  ctx.request.body
        console.log(params);
        let createTime = new Date()
        try {
            let [res] = await db.promise().query('insert into myorder(id,userId,userName,packageName,packagePrice,packageNum,payWay,totalPrice,cutPrice,staff,getScore,notes,goodsName,goodsPrice,goodsNum,createTime) values(?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?)',
           [params.id,params.userId,params.userName,params.packageName,params.packagePrice,params.packageNum,params.payWay,params.totalPrice,params.cutPrice,params.staff,params.getScore,params.notes,params.goodsName,params.goodsPrice,params.goodsNum,createTime])
            if(res.affectedRows > 0){
                ctx.body = {
                    code:200,
                    msg:'结算成功'
                }
               }
           } catch (error) {
                ctx.body = {
                    code:201,
                    msg:'结算失败请联系管理员' +error
                }
           }
    },
    // 会员充值
    VIPCONSUME: async (ctx, next) => {
        const params=  ctx.request.body
        let createTime = new Date()
        try {
            let [res] = await db.promise().query('insert into myorder(id,userId,userName,payWay,totalPrice,cutPrice,staff,notes,vipRecg,createTime) values(?,?,?,?,?,?,?,?,?,?)',
           [params.id,params.userId,params.userNamem,params.payWay,params.money,params.money,params.staff,params.notes,params.vipRecg,createTime])
           let [res1] = await db.promise().query('select money from viplist where id =? ',[params.userId])
           let [res2] =await db.promise().query('update viplist set money=? where id=?',[res1[0].money *1 + params.money *1,params.userId])
            if(res.affectedRows > 0   && res2.affectedRows > 0){
                ctx.body = {
                    code:200,
                    msg:'结算成功'
                }
               }
           } catch (error) {
                ctx.body = {
                    code:201,
                    msg:'结算失败请联系管理员' +error
                }
           }
    },
    CHANGESCORE: async (ctx, next) => {
        const params =ctx.request.body
        try {
            let res =await db.promise().query('update viplist set score=? where id=?',
            [params.score,params.id])
            if(res.affectedRows !== 0){
                ctx.body = {
                    code:200,
                    msg:"更新成功"
                }
            }else{
                ctx.body = {
                    code:500,
                    msg:"未找到该会员"
                }
            }
        } catch (error) {
            ctx.body = {
                code:500,
                msg:"更新失败"+error
            }
        }
    },
}